﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RedeSocial.DataAccess;
using RedeSocial.Web.UserControls;
using RedeSocial.ViewModels;
using Telerik.Web.UI;
using System.Xml;

namespace RedeSocial.Web
{
	public partial class UserMessages : BaseUserControl
	{
		#region properties...
		#endregion
		#region overrides...
		protected override void CreateViewModel()
		{

		}
		protected override void BindViewModel()
		{

		}
		protected override void BindLabels()
		{
			//lblError.Text = ResourceManager.GetResource(Constants.LOGINVIEW_INVALIDUSERMESSAGE, LoginViewModel.DefaultLanguage.Id);
		}
		#endregion
		#region properties...
		protected CreateUserViewModel CreateUserViewModel
		{
			get
			{
				return ViewModel as CreateUserViewModel;
			}
			set
			{
				ViewModel = value;
			}
		}
		#endregion
		#region event handles...
		protected void Page_Load(object sender, EventArgs e)
		{
			Response.Cache.SetCacheability(HttpCacheability.NoCache);
			if (!IsPostBack)
			{
			}
		}

		protected void btnSubmit_Click(object sender, EventArgs e)
		{
			int userId = (Session["session"] as Session).User.Id;
			using (RedeSocialEntities dataContext = new RedeSocialEntities())
			{
				int languageId = (Session["DefaultLanguage"] as Language).Id;
				NewsType newsType = dataContext.NewsTypeLanguages.Where(n => n.Language.Id == languageId).Select(n => n.NewsType).FirstOrDefault();
				User user = dataContext.Users.Include("File").Where(u => u.Id == userId).FirstOrDefault();
				News news = new News()
				{
					Message = txtShareMsg.Text.Trim(),
					Posted = DateTime.Now,
					Url = string.Empty,
					NewsType = newsType,
					User = user
				};
				dataContext.News.Add(news);
				dataContext.SaveChanges();
			}
			NewsEntity.DataBind();
			ListViewMessages.DataBind();
		}
		#endregion

		protected void NewsEntity_QueryCreated(object sender, QueryCreatedEventArgs e)
		{
			int userId = (Session["Session"] as Session).User.Id;
			var query = e.Query.Cast<News>();
			//e.Query = query.Where(n => n.User.Id == userId).Select(n => new { n.File, n.Id, n.Message, n.Posted, Comments = n.Comments.OrderBy(c => c.Posted) }).OrderByDescending(n => n.Posted);
			e.Query = query.Where(n => n.User.Id == userId && n.MainNews == null).OrderByDescending(n => n.Posted);
		}

		protected void ListViewComments_ItemInserting(object sender, Telerik.Web.UI.RadListViewCommandEventArgs e)
		{
			RadTextBox txt = e.ListViewItem.FindControl("txtComment") as RadTextBox;
			int newsId = Convert.ToInt32((e.ListViewItem.Parent.Parent as RadListView).Attributes["NewsId"]),
				userId = (Session["session"] as Session).User.Id;
			if (txt != null && txt.Text.Trim().Length > 0)
			{
				using (RedeSocialEntities dataContext = new RedeSocialEntities())
				{
					News mainNews = dataContext.News.Include("NewsType").Include("User").Where(n => n.Id == newsId).FirstOrDefault();
					dataContext.News.Attach(mainNews);
					User user = dataContext.Users.Include("File").Where(u => u.Id == userId).FirstOrDefault();
					News comment = new News()
					{
						Message = txt.Text.Trim(),
						Posted = DateTime.Now,
						Url = string.Empty,
						MainNews = mainNews,
						NewsType = mainNews.NewsType,
						User = user
					};
					mainNews.Comments.Add(comment);
					dataContext.SaveChanges();
				}
				NewsEntity.DataBind();
				ListViewMessages.DataBind();
			}
		}

		protected void btnSubmitComment_Click(object sender, EventArgs e)
		{
			RadTextBox txt = ((sender as RadButton).Parent.FindControl("txtComment") as RadTextBox);

			int newsId = Convert.ToInt32(txt.Attributes["NewsId"]);
			int userId = (Session["session"] as Session).User.Id;
			if (txt != null && txt.Text.Trim().Length > 0)
			{
				using (RedeSocialEntities dataContext = new RedeSocialEntities())
				{
					News mainNews = dataContext.News.Include("NewsType").Include("User").Where(n => n.Id == newsId).FirstOrDefault();
					dataContext.News.Attach(mainNews);
					User user = dataContext.Users.Include("File").Where(u => u.Id == userId).FirstOrDefault();
					News comment = new News()
					{
						Message = txt.Text.Trim(),
						Posted = DateTime.Now,
						Url = string.Empty,
						MainNews = mainNews,
						NewsType = mainNews.NewsType,
						User = user
					};
					mainNews.Comments.Add(comment);
					dataContext.SaveChanges();
				}
				NewsEntity.DataBind();
				ListViewMessages.DataBind();
			}

		}



		//protected void RadListView1_NeedDataSource(object sender, Telerik.Web.UI.RadListViewNeedDataSourceEventArgs e)
		//{
		//    using (var dataContext = new RedeSocialEntities())
		//    {
		//        RadListView1.DataSource = dataContext.News.Where(n=> n.User.Id == 1);
		//    }
		//}
	}
}